package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.*;
import com.wxxymakers.grademark.service.WxCollageService;
import com.wxxymakers.grademark.service.WxDepartmentService;
import com.wxxymakers.grademark.service.WxIncidentService;
import com.wxxymakers.grademark.service.WxItemService;
import com.wxxymakers.grademark.utils.Const;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/12/22 20:05
 * @DESCRIBE
 */
@Controller
@RequestMapping("/admin")
public class AdminViewController {

    @Autowired
    private WxCollageService collageService;

    @Autowired
    private WxDepartmentService departmentService;

    @Autowired
    private WxIncidentService incidentService;

    @Autowired
    private WxItemService itemService;

    /**
     * 评定事件
     */
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @GetMapping("/incident")
    public String incident() {
        return "admin/incident";
    }

    /**
     * 所有评定记录
     */
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @GetMapping("/data")
    public String data(ModelMap map, @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        List<WxIncident> incidents = incidentService.list();
        map.put("incidents", incidents);
        this.setCollegeDepts(map, user);
        Page<WxItem> items = itemService.page(1, Integer.MAX_VALUE, null);
        if (items != null) map.put("items", items.getContent());
        return "admin/data";
    }

    /**
     * 学院管理
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/college")
    public String college() {
        return "admin/college";
    }

    /**
     * 部门管理
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/dept")
    public String dept() {
        return "admin/dept";
    }

    /**
     * 评定条目管理
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/items")
    public String items() {
        return "admin/items";
    }

    /**
     * 添加部门页面
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/dept_add")
    public String deptAdd(ModelMap map) {
        map.put("dept", new WxDepartment());
        map.put("type", "add");
        return "admin/dept_add";
    }

    /**
     * 部门修改页面
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/dept_change/{id}")
    public String deptChange(@PathVariable int id, ModelMap map) {
        WxDepartment department = departmentService.byId(id);
        map.put("dept", department);
        map.put("type", "change");
        return "admin/dept_add";
    }

    /**
     * 职位管理页面
     */
    @RequiresRoles(value = {"admin"})
    @GetMapping("/duty")
    public String duty() {
        return "admin/duty";
    }

    /**
     * 用户管理
     */
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    @GetMapping("/users")
    public String users(ModelMap map, @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        setCollegeDepts(map, user);
        return "admin/users";
    }

    /**
     * 在model中设置colleges, depts字段
     * @param map 模型
     * @param user 当前登录的用户
     */
    private void setCollegeDepts(ModelMap map, WxUser user) {
        List empty = new ArrayList();
        map.put("colleges", empty);
        map.put("depts", empty);
        WxDepartment dept = new WxDepartment();
        if (user.getRole().getSign().equals("college-admin")) { // 普通管理
            Integer collageId = user.getCollageId();
            dept.setCollageId(collageId);
        } else {
            Page<WxCollege> content = collageService.page(1, 99999, null);
            if (content != null) map.put("colleges", content.getContent());
        }
        Page<WxDepartment> departments = departmentService.page(1, Integer.MAX_VALUE, dept);
        if (departments != null) map.put("depts", departments.getContent());
    }
}

